﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _066
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//新增按钮
        {
            string code = textBox1.Text;
            string name = textBox2.Text;
            string pwd = textBox3.Text;
            string sex = textBox4.Text;
            string age = textBox5.Text;
            string phone = textBox6.Text;

            //1.创建连接数据库字符串
            string constr = "server=.;database=MyDatabaseOne;user=sa;pwd=35683568";
            SqlConnection connection = new SqlConnection(constr);
            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd = connection.CreateCommand();
                //拼写语句
                cmd.CommandText = "insert into users values('" + code + "','" + name + "','" + pwd + "','" + sex + "','" + age + "','" + phone + "')";
                int count = cmd.ExecuteNonQuery();
                if(count>0)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }

                ////2.创建命令项目
                ////3.新增数据
                //string sqlstr = "insert into users values(04,'黄生','016676','男',28,12345)";
                //cmd.CommandText = sqlstr;
                ////4.执行命令
                //int i = cmd.ExecuteNonQuery();
                //Text = i.ToString();
                //if (i > 0)
                //{
                //    MessageBox.Show("执行成功");
                //}
                //else
                //{
                //    MessageBox.Show("执行失败");
                //}

            }
            catch (Exception ex)
            {
                Console.WriteLine("执行语句有误，请查验");
            }

        }
    }
}
